package com.asistencia.businness;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.asistencia.dao.interfaces.AssistanceDao;
import com.asistencia.domain.Assistance;
import com.asistencia.domain.Enrollment;
import com.asistencia.domain.Schedule;

@Service
public class AssistanceBusiness {

	@Autowired
	private AssistanceDao assistanceDao;

	@Autowired
	private Assistance assi;

	@Autowired
	private Enrollment enrollment;

	@Autowired
	private Schedule schedule;

	public int insert(String assistanceDate, String assistanceTime,
			String action, int idEnrollment, int idSchedule) {
		enrollment.setIdEnrollment(idEnrollment);
		schedule.setIdSchedule(idSchedule);

		assi.setAssistanceDate(assistanceDate);
		assi.setAssistanceTime(assistanceTime);
		assi.setAction(action);
		assi.setMyEnrollment(enrollment);
		assi.setMySchedule(schedule);

		try {
			return assistanceDao.insert(assi);

		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	public int updateAssistance(Integer idAssistance, String assistanceTime,String action) {
		assi.setAction(action);
		assi.setIdAsistence(idAssistance);
		assi.setAssistanceTime(assistanceTime);
		try {
			return assistanceDao.update(assi);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	

	public Assistance getByScheduleEnrollment(Integer idSchedule, Integer idEnrollment) {
		try {
			assi = assistanceDao.getByScheduleEnrollment(idSchedule,idEnrollment);
			return assi;
		}

		catch (DataAccessException e) {
			e.printStackTrace();
			return null;
		}
	}

	public Assistance getById(Integer idAssistance) {
		try {
			assi = assistanceDao.getById(idAssistance);
			return assi;
		}

		catch (DataAccessException e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<Assistance> lisByEnrollment(Integer idEnrollment) {
		try {
			assistanceDao.lisByEnrollment(idEnrollment);
			List<Assistance> lstById = assistanceDao
					.lisByEnrollment(idEnrollment);
			return lstById;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
